package com.czp.ssm.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.czp.ssm.bean.Msg;
import com.czp.ssm.bean.Plane;
import com.czp.ssm.bean.PlaneOrder;
import com.czp.ssm.service.PlaneService;

@Controller
@RequestMapping("/planeController")
public class PlaneController {
	
	@Autowired
	private PlaneService planeService;
	
	/**
	 * 转向飞机票页面
	 * @return   
	 */
	@RequestMapping("/toPlaneIndex")
	public String toPlaneIndex() {
		return "/foreground/plane/plane_index";
	}
	

	/**
	 * 跳转到航班页面，带着参数
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/toPlaneList" , method=RequestMethod.GET)
	public ModelAndView toPlaneList(Plane plane) {
		ModelAndView modelAndView = new ModelAndView();
		List<Plane> planeList = planeService.getPlaneListByCondition(plane);
		modelAndView.setViewName("/foreground/plane/plane_List");
		modelAndView.addObject(plane);
		modelAndView.addObject(planeList);
		return modelAndView;
	}
	
	/**
	 * 根据条件查询对应的航班信息
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/findPlaneList" , method=RequestMethod.POST)
	@ResponseBody
	public Msg findPlaneList(Plane plane) {
		List<Plane> planeList = planeService.getPlaneListByCondition(plane);
		return Msg.success().add("planeList", planeList);
	}
	
	/**
	 * 根据条件查询对应的航班信息
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/getPlaneOrderEdit" , method=RequestMethod.GET)
	public ModelAndView getPlaneOrderEdit(@RequestParam("planeId")String planeId,@RequestParam("aircraftId")String aircraftId) {
		ModelAndView modelAndView = new ModelAndView();
		Plane plane = planeService.getPlaneByCondition(planeId,aircraftId);
		modelAndView.addObject("plane", plane);
		modelAndView.setViewName("/foreground/plane/plane_book");
		return modelAndView;
	}
	
	/**
	 * 提交订单
	 * @param planeCondition
	 * @return
	 */
	@RequestMapping( value="/submitPlaneOrder" , method=RequestMethod.POST)
	@ResponseBody
	public Msg submitPlaneOrder(PlaneOrder planeOrder) {
		int flag = planeService.savePlaneOrder(planeOrder);
		if (flag<0) {
			return Msg.success();
		}
		return Msg.fail();
	}
	
	
	/**
	 * 飞机票订单列表
	 * @return
	 */
	@RequestMapping("/toPlaneOrderList")
	public ModelAndView toPlaneOrderList() {
		List<PlaneOrder> planeOrderList = planeService.findAllPlaneOrders();
		System.out.println(planeOrderList);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("planeOrderList",planeOrderList);
		modelAndView.setViewName("/foreground/plane/plane_orderList");
		return modelAndView;
	}
	
	
}
